#pragma once

#include "Poller.h"
#include "Timestamp.h"

#include <vector>
#include <sys/epoll.h>

class Channel;

//epoll的使用，采用LT模式
class EPollPoller : public Poller
{
public:
    EPollPoller(EventLoop *loop);
    ~EPollPoller();

    // override要求子类必须实现对父类的重写
    Timestamp poll(int timeoutMs, ChannelList *activeChannels) override;
    void updateChannel(Channel *channel) override;
    void removeChannel(Channel *channel) override;

private:
    static const int kInitEventListsSize = 16;

    //将就绪的事件传给Channel中的revents，并写入EventLoop中的activeChannels
    void fillActiveChannels(int numEvents, ChannelList *activeChannels) const;

    //更新epoll的监听事件
    void update(int op, Channel *channel);

    using EventList = std::vector<struct epoll_event>;

    int epfd_;
    //存储Poller关心的事件，当然这写事件和对应的fd、Channel 都以 struct epoll_event 的形式被存储
    EventList events_;
};